package edu.tec.mty.test;

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import edu.tec.mty.core.Commands;
import edu.tec.mty.core.Controller;
import edu.tec.mty.core.modules.Messages;
import edu.tec.mty.core.modules.Self;
import edu.tec.mty.core.modules.models.BallModelFactory;
import edu.tec.mty.core.modules.models.Message;
import edu.tec.mty.core.modules.models.Position;

public class Manual {

	public static void main(String[] args) {
		System.out.println("Manual v0.1");
		Scanner scanner = new Scanner(System.in);
		//Each line for each command
		Pattern p = Pattern.compile("\\(((move\\s\\-?\\d+\\.\\d+\\s\\-?\\d+\\.\\d+)|" + 
										"(dash\\s\\-?\\d+\\.\\d+\\s\\-?\\d+\\.\\d+)|" +
										"(turn\\s\\-?\\d+)|" + 
										"(neck\\s\\-?\\d+)|" +
										"(change_view\\s(narrow|normal|wide)\\s(high|low))|" + 
										"(say\\s[0-9a-zA-Z]+)|" +
										"(hear)|" +
										"(position)|" +
										"(ball)|" +
										"(exit))\\)");
		Controller controller = new Controller();
		controller.setTeamName("Nemesis");
		controller.setTypeOfAgent('p');
		try {
			System.out.println("Connecting...");
			controller.connect("localhost");
			System.out.println("Connected!");
			Commands commands = controller.getReferenceToCommands();
			Self player = controller.getReferenceToPlayer();
			BallModelFactory ballModel = controller.getReferenceToBallModel();
			Messages messages = controller.getReferenceToMessages();
			try {
				//String command = scanner.next(p);
				boolean flag = false;
				String command = "";
				do {
					System.out.print("Command: ");
					command = scanner.nextLine();
					Matcher matcher = p.matcher(command);
					flag = matcher.matches();
				} while (!flag);
				while (!command.equals("(exit)")) {
					System.out.println("Sending " + command);
					command = command.substring(1, command.length() - 1);
					String[] tokens = command.split(" ");
					double dArg0 = 0.0, dArg1 = 0.0;
					int iArg = 0;
					char firstChar = command.charAt(0);
					Position playerPosition = player.getPosition();
					Position ballPosition = ballModel.getBallModel().getBall().getCurrentPosition();
					switch (firstChar) {
					case 'm':
						dArg0 = Double.parseDouble(tokens[1]);
						dArg1 = Double.parseDouble(tokens[2]);
						commands.move(dArg0, dArg1);
						break;
					case 'd':
						dArg0 = Double.parseDouble(tokens[1]);
						dArg1 = Double.parseDouble(tokens[2]);
						commands.dash(dArg0, dArg1);
						break;
					case 't':
						iArg = Integer.parseInt(tokens[1]);
						commands.turn(iArg);
						break;
					case 'n':
						iArg = Integer.parseInt(tokens[1]);
						commands.turnNeck(iArg);
						break;
					case 'c':
						commands.changeView(tokens[1], tokens[2]);
						break;
					case 'p':
						System.out.println("x: " + playerPosition.getX() + " y: " + playerPosition.getY() + " angle: " + playerPosition.getBodyAngle());
						break;
					case 'b':
						System.out.println("x: " + ballPosition.getX() + " y: " + ballPosition.getY());
						System.out.println("seeing ball at: " + playerPosition.getAngleFromBodyTo(ballPosition));
						break;
					case 's':
						commands.say(tokens[1]);
						break;
					case 'h':
						try {
							Message msg = messages.getNextPlayerMessage();
							System.out.println(msg.getDirection() + " : " + msg.getMessage());
						} catch (Exception e) {
							System.out.println("message not heared");
						}
						break;
					default:
						break;
					}
					try {
						do {
							System.out.print("Command: ");
							command = scanner.nextLine();
							Matcher matcher = p.matcher(command);
							flag = matcher.matches();
						} while (!flag);
					} catch (Exception e) {
						command = "(exit)";
					}
				}
			} catch (Exception e) {
				System.err.println(e);
			}
			System.out.println("exit");
			controller.disconnect();
			System.out.println("Bye");
		} catch (Exception e) {
			System.err.println(e);
			if (controller.isConnected()) {
				controller.disconnect();
				System.out.println("Bye");
			}
		}
		scanner.close();
	}
	
}
